package com.greyshark.sanotrive.splash

import android.graphics.drawable.Animatable
import android.graphics.drawable.Drawable
import android.view.View.GONE
import android.view.View.VISIBLE
import android.widget.ImageView
import android.widget.LinearLayout
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.common.base.app.extras.OnImageLoadListener
import com.common.base.app.extras.loadYX
import com.common.base.utils.ImgUrlUtils
import com.facebook.drawee.controller.ControllerListener
import com.facebook.drawee.view.SimpleDraweeView
import com.facebook.imagepipeline.image.ImageInfo
import com.greyshark.sanotrive.R


/**
 *
 * 作者：sosou
 *
 * 版本：1.0
 *
 * 创建日期：15/2/2023
 *
 * 描述：
 *
 * 修订历史：
 *
 */
class SplashPicListAdapter(mWidth: Int = 0) :
    BaseQuickAdapter<String, BaseViewHolder>(R.layout.item_splash_pic_layout) {

    private var curWidth: Int = mWidth

    private var listener: OnSplashPicCompleteListener? = null

    override fun convert(helper: BaseViewHolder, item: String) {

        //屏幕比例
        helper.getView<ImageView>(R.id.iv_item_img).loadYX(
            ImgUrlUtils.getWidthAutoScale(
                ImgUrlUtils.TYPE_AL,
                item,
                720, "webp"
            ), placeholder = R.drawable.common_transparent_bg, listener = object :
                OnImageLoadListener {
                //        helper.getView<ImageView>(R.id.iv_item_img).load(item, round = 0f,placeholder = R.drawable.common_transparent_bg, listener = object: OnImageLoadListener {
                override fun onSuccess(bitmap: Drawable?) {
                    //图片加载成功 设置为可见
                    helper.getView<ImageView>(R.id.iv_item_img).visibility = VISIBLE
                    listener?.onHasSplashPicComplete()
                }

                override fun onFail(msg: String) {
                    helper.getView<ImageView>(R.id.iv_item_img).visibility = GONE
                }
            })
//        val iv = helper.getView<SimpleDraweeView>(R.id.iv_item_img)
//        val controller: DraweeController = Fresco
//            .newDraweeControllerBuilder()
////            .setUri(item)
//            .setUri(ImgUrlUtils.getWidthAutoScale(
//                ImgUrlUtils.TYPE_AL,
//                item,
//                720,"webp"
//            ))
//            .setControllerListener(
//                MyControllerListener(
//                    iv,
//                    getScreenWidth()
//                )
//            )
//            .build()
//        iv.controller = controller
    }


    internal class MyControllerListener(
        private val view: SimpleDraweeView,
        private val screenWidth: Int,
    ) :
        ControllerListener<ImageInfo> {
        private var isMeasured = false
        override fun onFailure(arg0: String?, arg1: Throwable?) {}
        override fun onIntermediateImageFailed(arg0: String?, arg1: Throwable?) {}
        override fun onIntermediateImageSet(arg0: String?, imageInfo: ImageInfo?) {}
        override fun onRelease(arg0: String?) {}
        override fun onSubmit(arg0: String?, arg1: Any?) {}
        override fun onFinalImageSet(id: String?, imageInfo: ImageInfo?, animatable: Animatable?) {
            measured(imageInfo)
        }

        private fun measured(info: ImageInfo?) {
            info?.let {
                if (isMeasured) {
                    return
                }
                isMeasured = true

                // 获取图片真实宽高
                val width = it.width
                val height = it.height

                // 获取图片显示控件宽度,基于宽度适配图片
                val viewWidth = screenWidth
                val viewRatio = viewWidth * 1.0f / width
                view.layoutParams =
                    LinearLayout.LayoutParams(viewWidth, (height * viewRatio).toInt())
            }
        }
    }


    /**
     * @param listener
     */
    fun setListener(listener: OnSplashPicCompleteListener?) {
        this.listener = listener
    }

    /**
     *
     */
    interface OnSplashPicCompleteListener {
        fun onHasSplashPicComplete()
    }
}